package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ERP.BomInfoErp;
import com.ruoyi.system.domain.SetModel;
import com.ruoyi.system.domain.function.ProdPlan;
import com.ruoyi.system.mapper.ProdPlanMapper;
import com.ruoyi.system.mapper.SetModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.mapper.BomInfoErpMapper;
import com.ruoyi.system.service.IBomInfoErpService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * erp工单bom清单详情Service业务层处理
 *
 * @author 江小熊
 * @date 2024-09-30
 */
@Service
public class BomInfoErpServiceImpl extends ServiceImpl<BomInfoErpMapper, BomInfoErp> implements IBomInfoErpService {
    @Autowired
    private SetModelMapper setModelMapper;
    @Autowired
    private ProdPlanMapper prodPlanMapper;

    @Transactional
    @Override
    public String importBom(List<BomInfoErp> bomList, boolean isUpdateSupport, String operName,String scddErpId,String taskTon) {
        if (StringUtils.isNull(bomList) || bomList.isEmpty()) {
            throw new CustomException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (BomInfoErp bom : bomList) {
            //验证是否存在
            LambdaQueryWrapper<BomInfoErp> lqw = new LambdaQueryWrapper<>();
            //queryWrapper.eq("code",mac.getCode());
            if (StringUtils.isNotBlank(scddErpId)){
                lqw.eq(BomInfoErp::getScddErpId ,scddErpId);
            }
            if (StringUtils.isNotBlank(bom.getChildCode())){
                lqw.eq(BomInfoErp::getChildCode ,bom.getChildCode());
            }
            if (StringUtils.isNotBlank(taskTon)){
                lqw.eq(BomInfoErp::getTaskTon ,taskTon);
            }
            List<BomInfoErp> areaModels = baseMapper.selectList(lqw);
            if (CollectionUtils.isEmpty(areaModels)) {
                bom.setScddErpId(scddErpId);
                bom.setCreateBy(SecurityUtils.getUsername());
                bom.setCreateTime(new Date());
                bom.setTaskTon(taskTon);
                baseMapper.insert(bom);
                successNum++;
                //successMsg.append("<br/>" + successNum + "、BOM维护： " + mac.getCode()  + " 导入成功");
            }else {
                failureNum++;
                //failureMsg.append("<br/>" + failureNum + "、BOM维护： " + mac.getCode()  + " 已经存在");
            }
        }

        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
